home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_065 / cookie / cookie.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  110 lines

  1. /************************************************************************/
  2. /* Filename: COOKIE.C                    Main Program    */
  3. /*                                    */
  4. /* Author: Rick Stevens, Small Scale Systems of Southern California    */
  5. /* Last Edit Date: 4 April 1987                        */
  6. /*                                    */
  7. /* Revisions:                                */
  8. /*                                    */
  9. /*    Version 1.0f -    Edited to use logical assign COOKIES: and to    */
  10. /*            run under Lattice 3.10.  4-Apr-87, Fred Fish    */
  11. /*    Version 1.0  -    First release of program. No modifications    */
  12. /*                                    */
  13. /* This program requires the file "COOKIE.DAT" to be in the "COOKIES:"    */
  14. /* directory or else you will be given the message:            */
  15. /*    "All the cookies are  broken".                    */
  16. /*                                    */
  17. /* This program released to the public domain by Rick Stevens, Small    */
  18. /* Scale Systems of Southern California. It may be copied and        */
  19. /* distributed freely without fee. The copyright notice MUST be        */
  20. /* included in all future copies.                    */
  21. /*                                    */
  22. /* Written in Lattice C, Version 3.03B.                    */
  23. /*                                    */
  24. /* Copyright (C) 1987, Richard P. Stevens, II and            */
  25. /*                Small Scale Systems of Southern California    */
  26. /************************************************************************/
  27.  
  28. #include <stdio.h>            /* Main header file        */
  29. #include <fcntl.h>            /* File control header file    */
  30. #define FILSIZ 37028            /* Current COOKIE.DAT size    */
  31.  
  32. #ifdef ORIGCODE
  33. extern int Enable_Abort;
  34. #endif
  35.  
  36. main() {
  37.  
  38.     int fd;                /* File descriptor for data file*/
  39.     int ok;                /* OK flag            */
  40.     int temp;                /* Scratch            */
  41.     int i;                /* Scratch            */
  42.     char ch;                /* Scratch            */
  43.     char buf[600];            /* Destination for fortune    */
  44.     unsigned long seed[3];        /* Seed for random number    */
  45.     long pos;                /* For file positioning        */
  46.  
  47.     long lseek();            /* LSEEK() returns a long    */
  48. #ifdef ORIGCODE
  49.     long lrand48();            /* So does LRAND48()        */
  50.     Enable_Abort = 1;            /* Enable CTRL/C & CTRL/D abort    */
  51. #else
  52.     int rand ();
  53. #endif
  54.     
  55.     fd = open("COOKIES:cookie.dat",O_RDONLY);    /* Open the data file    */
  56.     if (fd <= 0) {
  57.         printf("\nSorry, all of the cookies are broken!\n");
  58.     exit(0);
  59.     }
  60.     
  61.     ok = 0;
  62.     DateStamp(seed);            /* Get a seed value        */
  63. #ifdef ORIGCODE
  64.     srand48(seed[2]);            /* Seed the random number gen.    */
  65. #else
  66.     srand ((unsigned int) seed[2]);
  67. #endif
  68.     while (!ok) {            /* While there's no fortune...    */
  69. #ifdef ORIGCODE
  70.     pos = lrand48();        /* Get a random number        */
  71. #else
  72.     pos = rand ();
  73. #endif
  74.     pos = (long) (pos/FILSIZ);    /* Make pos more reasonable    */
  75.     seed[0] = lseek(fd,pos,0);    /* Can we seek to it?        */
  76.         if (seed[0] <= 0L)
  77.         continue;            /* Nope, back to WHILE (!OK)    */
  78.  
  79.     while ((temp = read(fd,&ch,1)) > 0) {    /* Find the next <FF>    */
  80.         if (ch == 0x0c)        /* Was it a form feed?        */
  81.             break;            /* Yes, stop the search        */
  82.     }
  83.  
  84.     if (temp <= 0)            /* Did we get a <FF> really?    */
  85.         continue;            /* Nope, back to WHILE (!OK)    */
  86.         
  87.     i = 0;                /* Clear the buffer counter    */
  88.     while ((temp = read(fd,&ch,1)) > 0) {    /* Read fortune    */
  89.         if (ch == 0x0c) {        /* Did we read a form feed?    */
  90.             buf[i] = '\0';        /* Yes, stuff in a null char.    */
  91.         ok = 1;            /* Set the OK flag        */
  92.         break;            /* Break out of read loop    */
  93.         } else {            /* Not a form feed, so...    */
  94.         buf[i] = ch;        /* ...stuff it into the buffer    */
  95.             ++i;            /* increment the counter    */
  96.         }
  97.     }
  98.     if (ok)
  99.         break;
  100.     }                    /* End of WHILE (!OK) loop    */
  101.  
  102.     temp = close(fd);            /* Close the data file        */
  103.     printf("(C) 1987, Rick Stevens and ");
  104.     printf("Small Scale Systems of Southern California");
  105.     printf("\n%s\n",buf);        /* Print out the buffer        */
  106. }
  107. /************************************************************************/
  108. /*     End of file: COOKIE.C                        */
  109. /************************************************************************/
  110.